package com.moujitx.pickpeach;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class pick extends AppCompatActivity implements View.OnClickListener{

    private Button returnHome, peach1, peach2, peach3, peach4, peach5, peach6;
    private int count=0;
    Toast toast = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick);
        init();
    }

    private void init(){
        peach1 = findViewById(R.id.peach1);
        peach2 = findViewById(R.id.peach2);
        peach3 = findViewById(R.id.peach3);
        peach4 = findViewById(R.id.peach4);
        peach5 = findViewById(R.id.peach5);
        peach6 = findViewById(R.id.peach6);
        returnHome = findViewById(R.id.returnHome);

        peach1.setOnClickListener(this);
        peach2.setOnClickListener(this);
        peach3.setOnClickListener(this);
        peach4.setOnClickListener(this);
        peach5.setOnClickListener(this);
        peach6.setOnClickListener(this);
        returnHome.setOnClickListener(this);
    }

    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.peach1: info(peach1);break;
            case R.id.peach2: info(peach2);break;
            case R.id.peach3: info(peach3);break;
            case R.id.peach4: info(peach4);break;
            case R.id.peach5: info(peach5);break;
            case R.id.peach6: info(peach6);break;
            case R.id.returnHome: returnHome();break;
        }
    }

    private void info(Button btn){
        count++;
        btn.setVisibility(View.INVISIBLE);

        if(toast != null) toast.cancel();
        toast = Toast.makeText(pick.this,"摘到"+count+"个桃子",Toast.LENGTH_SHORT);
        toast.show();
    }

    private void returnHome(){
        Intent intent = new Intent();
        intent.putExtra("count",count);
        setResult(1,intent);
        Log.i("count", String.valueOf(count));
        pick.this.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
            returnHome();
        }
        return false;
    }
}